package com.handinfo.android.uicontrols.controls;

import java.util.concurrent.CopyOnWriteArrayList;

import android.graphics.Bitmap;
import android.graphics.PointF;
import android.view.MotionEvent;

import com.handinfo.android.core.graphics.DWGraphics;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWGuideObject;
import com.handinfo.android.utils.Tools;

/**
 * 标签页容器
 * @author handinfo
 */
public class DWTabControl extends DWControl
{
	private CopyOnWriteArrayList<DWTabPage> m_items = new CopyOnWriteArrayList<DWTabPage>();
//	private DWFont m_font = DWFont.getDefaultFont();
	private final String TAB_PAGE_NAME = "TabPage";
	private int w_tag = 0;//标签单个宽度
	private int h_tag = 0;//标签单个高度
	private int w_amount_tag = 0;//标签页总宽度
//	private final int H_TAB_PAGE_TAG = DWControlsManager.H_ALL_FONT + 10;

	private int m_page_index = 0;
//	private float m_page_offset_x = 0;
	private float m_page_offset_y = 0;
	private float m_tag_offset_x = 0;
//	private float m_tag_offset_y = 0;
	private Bitmap img_tag_noSelected = null;
	private Bitmap img_tag_selected = null;
	private Bitmap img_jiantou_l = null;
	private Bitmap img_jiantou_r = null;
	
	private int m_guide_index = 0;//用户指引下标

	/**
	 * 创建一个标签页容器
	 * @param w
	 * @param h
	 */
	public DWTabControl(Bitmap noSelected, Bitmap selected, int w, int h)
	{
		if (noSelected == null || selected == null) {
			throw new NullPointerException("DWTabControl Bitmap is null!");
		}
		setShowWideHigh(w, h);
		img_tag_noSelected = noSelected;
		img_tag_selected = selected;
		w_tag = img_tag_selected.getWidth();
		h_tag = img_tag_selected.getHeight();
		img_jiantou_l = UIWindows.createImage("/img/newui/jiantou_2.gnp");
		img_jiantou_r = UIWindows.createImage("/img/newui/jiantou_1.gnp");
	}

	public void render(DWGraphics g)
	{
		g.setClip(m_show_x, m_show_y, m_show_w + 1, m_show_h + 1);
		renderRect(g);
		DWTabPage tabPage = null;
		m_deviant_y = (int) m_page_offset_y;
		int tag_ox = (int) m_tag_offset_x;
		for (int i = 0; i < m_items.size(); i++) {
			tabPage = (DWTabPage) m_items.get(i);
			if (tabPage.m_is_show) {
				g.setClip(m_show_x, m_show_y, m_show_w + 1, m_show_h + 1);
				tabPage.renderTag(g, img_tag_selected, img_tag_noSelected, tabPage.getName(),
						m_show_x + i * w_tag - tag_ox, m_show_y, w_tag, h_tag, tabPage.getSelect());
				tabPage.renderLimits(g, m_show_x, m_show_y, m_show_w, m_show_h, (int)m_deviant_x, (int)m_deviant_y);
			}
		}
		
		if(w_amount_tag>m_show_w){
			g.setClip(m_show_x-img_jiantou_l.getWidth()-5, m_show_y, m_show_w+img_jiantou_l.getWidth()*2+10 + 1, m_show_h + 1);
			time++;
			if(time<3){
				loop = 0;
			}else if(time<6){
				loop = 1;
			}else{
				time = 0;
			}
			
			if (m_tag_offset_x  == 0) {
				g.drawBitmap(img_jiantou_r, m_show_x+m_show_w+loop*5, m_show_y, 0);
			}
			else if (m_tag_offset_x + m_show_w >= w_amount_tag) {
				g.drawBitmap(img_jiantou_l, m_show_x-img_jiantou_l.getWidth()-loop*5,m_show_y, 0);
			}else{
				g.drawBitmap(img_jiantou_r, m_show_x+m_show_w+loop*5, m_show_y, 0);
				g.drawBitmap(img_jiantou_l, m_show_x-img_jiantou_l.getWidth()-loop*5,m_show_y, 0);
			}
		}
		renderGuide(g);
	}
	public int loop=0;
	public int time = 0;
	public void renderGuide(DWGraphics g) {
		if (m_is_guide) {
			DWGuideObject guide = DWControlsManager.getInstance().m_guides.getCurrentGuideObject();
			if (guide != null) {
				guide.draw(g, m_show_x + w_tag * m_guide_index, m_show_y, w_tag, h_tag);
			}
		
		}
	}
	/**
	 * 得到标签高度
	 * @return
	 */
	public int getTagHeight()
	{
		return h_tag;
	}
	
//	public void setFont(DWFont font) {
//		m_font = font;
//	}

//	/**
//	 * 绘制标签
//	 * @param g
//	 * @param name
//	 * @param x
//	 * @param y
//	 * @param w
//	 * @param h
//	 * @param select
//	 */
//	private void renderTag(DWGraphics g, String name, int x, int y, int w, int h, boolean select)
//	{
//		int tx = x + w / 2;
//		int ty = y + h / 2;
//		int nw = (int)m_font.stringWidth(name) / 2;
//		if (select) {
//			if (img_tag_selected != null && name != null) {
//				g.drawBitmap(img_tag_selected, tx, y + h, Tools.HCENTER_BOTTOM);
//				g.drawShadowString(m_font, name, DWControlsManager.COLOR_STROKE, Tools.WHITE, tx - nw, ty - (m_font.getHeight() / 2), Tools.LEFT_TOP);
//			}
//		}
//		else {
//			if (img_tag_selected != null && name != null) {
//				g.drawBitmap(img_tag_noSelected, tx, y + h, Tools.HCENTER_BOTTOM);
//				g.drawShadowString(m_font, name, DWControlsManager.COLOR_STROKE, Tools.WHITE, tx - nw, ty - (m_font.getHeight() / 2), Tools.LEFT_TOP);
//			}
//		}
//	}

	public void logic()
	{
		for (int i = 0; i < m_items.size(); i++) {
			m_items.get(i).logic();
		}
	}
	
	/**
	 * 设置某一个Page为选择状态
	 * @param index
	 */
	public void setSelectPage(int index) {
		if (index > -1 && index < m_items.size()) {
			for (int i = 0; i < m_items.size(); i++) {
				if (index == i) {
					m_items.get(i).setSelect(true);
				} else {
					m_items.get(i).setSelect(false);
				}
			}
			m_page_index = index;
		}
	}

	/**
	 * 得到选择页的下标
	 * @return
	 */
	public int getPageIndex()
	{
		return m_page_index;
	}

	/**
	 * 设置标签页坐标
	 * @param x
	 * @param y
	 */
	public void setPoint(int x, int y)
	{
		super.setPoint(x, y);
	}

	/**
	 * 设置标签页宽高
	 * @param w
	 * @param h
	 */
	public void setShowWideHigh(int w, int h)
	{
		super.setShowWideHigh(w, h);
	}

	/**
	 * 添加一组标签页
	 * @param controls
	 */
	public void addTabPages(DWTabPage[] tabPages)
	{
		if (tabPages != null) {
			for (int i = 0; i < tabPages.length; i++) {
				addTabPage(tabPages[i]);
			}
		}
	}

	/**
	 * 添加一个标签页
	 * @param control
	 */
	public void addTabPage(DWTabPage tabPage)
	{
		if (tabPage != null) {
			if (tabPage.isNoName()) {
				tabPage.setName(TAB_PAGE_NAME + m_items.size());
			}
			tabPage.setShowWideHigh(m_show_w, m_show_h - h_tag);
			tabPage.setNearAnchor(this, Tools.LEFT_TOP, Tools.LEFT_TOP, 0, h_tag);
			tabPage.setPointUn(tabPage.m_show_x - m_show_x, tabPage.m_show_y - m_show_y);
			tabPage.setRelyControl(this);
			tabPage.refreshControl();
			if (m_items.size() < 1) {
				tabPage.setSelect(true);
			}
			m_items.add(tabPage);
			w_amount_tag = m_items.size() * w_tag;
		}
	}
	
	public void setGuidePageIndex(int index) {
		m_guide_index = index;
	}
	
	/**
	 * 删除一个标签页
	 * @param index
	 */
	public void removeTabPage(int index) {
		if (index > -1 && index < m_items.size()) {
			DWTabPage tp = m_items.remove(index);
			if (tp.getSelect() && m_items.size() > 0) {
				m_items.get(0).setSelect(true);
			}
		}
	}
	
	/**
	 * 删除一个标签页
	 * @param tabPage
	 */
	public void removeTabPage(DWTabPage tabPage) {
		if (tabPage != null) {
			if (m_items.remove(tabPage)) {
				if (tabPage.getSelect() && m_items.size() > 0) {
					m_items.get(0).setSelect(true);
				}
			}
		}
	}
	/**
	 * 移除所有TabPage
	 */
	public void removeAllTabPage() {
		m_items.clear();
	}

	public void resetTabPageSelect()
	{
		for (int i = 0; i < m_items.size(); i++) {
			m_items.get(i).setSelect(false);
		}
	}

	/**
	 * 是否点击的标签页
	 * @return
	 */
	public boolean isClickTabPageTag(Float y)
	{
		return y - m_show_y <= h_tag;
	}

	public boolean doDown(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			if (isClickTabPageTag(point.y) && w_amount_tag > m_show_w) {
				
			} else {
				PointF newPoint = new PointF(point.x - m_show_x, point.y - m_show_y);
				if (m_items.get(m_page_index).doDown(newPoint)) {
					return true;
				}
			}
			return true;
		}
		return false;
	}
	public boolean doLongPress(PointF point)
	{
		if (checkPointOnRect(point.x, point.y)) {
			if (isClickTabPageTag(point.y) && w_amount_tag > m_show_w) {
				
			} else {
				PointF newPoint = new PointF(point.x - m_show_x, point.y - m_show_y);
				if (m_items.get(m_page_index).doLongPress(newPoint)) {
					return true;
				}
			}
		}
		return false;
	}
	
	public boolean doScroll(PointF start, PointF end, float distanceX, float distanceY)
	{
		if (checkPointOnRect(start.x, start.y)) {
			if (isClickTabPageTag(start.y) && w_amount_tag > m_show_w) {
				float b = w_amount_tag / m_show_w;
				m_tag_offset_x += distanceX / b;
				if (m_tag_offset_x < 0) {
					m_tag_offset_x = 0;
				}
				else if (m_tag_offset_x + m_show_w > w_amount_tag) {
					m_tag_offset_x = w_amount_tag - m_show_w;
				}
			}
			else {
				PointF newSrart = new PointF(start.x - m_show_x, start.y - m_show_y);
				PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
				if (m_items.get(m_page_index).doScroll(newSrart, newEnd, distanceX, distanceY)) {
					return true;
				}
			}
		}
		return false;
	}

	public boolean doDrag(PointF start, PointF end, float velocityX, float velocityY)
	{
		if (checkPointOnRect(end.x, end.y)) {
			PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
			PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
			if (isClickTabPageTag(start.y)) {
				
			} else {
				if (m_items.get(m_page_index).doDrag(newStart, newEnd, velocityX, velocityY)) {
					return true;
				}
			}
		}
		return false;
	}
	public boolean doFling(PointF start, PointF end, float velocityX, float velocityY)
	{
		PointF newStart = new PointF(start.x - m_show_x, start.y - m_show_y);
		PointF newEnd = new PointF(end.x - m_show_x, end.y - m_show_y);
		if (isClickTabPageTag(start.y)) {
			
		} else {
			if (m_items.get(m_page_index).doFling(newStart, newEnd, velocityX, velocityY)) {
				return true;
			}
		}
		return false;
	}
	public boolean doClick(float x, float y)
	{
		if (checkPointOnRect(x, y)) {
			if (isClickTabPageTag(y)) {
				int index = (int) ((x - m_show_x + m_tag_offset_x) / w_tag);
				if (index > -1 && index < m_items.size()) {
					if (m_guide_index == index) {
						setGuide(false);
					}
					resetTabPageSelect();
					m_items.get(index).setSelect(true);
					m_page_index = index;
					if (m_listener != null) {
						m_listener.OnClick();
					}
					return true;
				}
			}
			else {//点击tabpage
				if (m_items.get(m_page_index).doClick(x - m_show_x, y - m_show_y)) {
					return true;
				}
			}
		}
		return false;
	}
	public boolean onTouch(MotionEvent event, PointF point) {
		if (checkPointOnRect(point.x, point.y)) {
			if (isClickTabPageTag(point.y)) {
			}
			else {
				if (m_items.get(m_page_index).onTouch(event, new PointF(point.x - m_show_x, point.y - m_show_y))) {
					return true;
				}
			}
		}
		return false;
	}
}
